package dcc.component;

import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.swingbinding.JTableBinding;
import org.jdesktop.swingbinding.SwingBindings;

import dcc.mbg.model.BaseInfo;
import dcc.service.CommonService;
import dcc.tools.BeanFactory;
import dcc.tools.Tools;
import dcc.ui.SearchWindow;

public class SearchResultTable extends JTable {

	private List<BaseInfo> records;

	private BaseInfo getSelectedRecord() {
		int rowIndex = this.getSelectedRow();
		if (-1 == rowIndex) {
			return null;
		} else {
			BaseInfo baseInfo = new BaseInfo();
			baseInfo.setShiBieHao(this.getModel().getValueAt(rowIndex, 1)
					.toString());
			baseInfo.setLeiBie(this.getModel().getValueAt(rowIndex, 0)
					.toString());
			return baseInfo;
		}
	}

	private SearchWindow searchWindow;

	private void hideSearchWindow() {
		this.searchWindow.setVisible(false);
	}

	public SearchResultTable(SearchWindow searchWindow) {
		this.searchWindow = searchWindow;
		setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		setFont(new Font("Microsoft YaHei", Font.PLAIN, 16));
		this.setRowHeight(30);
		this.getTableHeader().setFont(new Font("Microsoft YaHei", Font.PLAIN, 16));

		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (2 == e.getClickCount()) {
					BaseInfo baseInfo = ((CommonService) BeanFactory
							.getBean("commonService"))
							.getBaseInfoByShiBieHaoAndLeiBie(getSelectedRecord());
					String talentType = Tools.get().getTalentTypeSymbol(
							baseInfo.getLeiBie());
					Tools.get().getManagerGroup()
							.bindModelToManager(talentType, baseInfo);
					Tools.get().getTaskPane()
							.callInformationManageButton(talentType);
					hideSearchWindow();
				}
			}
		});
	}

	@Override
	public boolean isCellEditable(int row, int column) {
		return false;
	}

	public void updateData(List<BaseInfo> records) {
		this.records = records;
		initDataBindings();
	}

	protected void initDataBindings() {
		JTableBinding<BaseInfo, List<BaseInfo>, JTable> jTableBinding = SwingBindings
				.createJTableBinding(UpdateStrategy.READ, records, this);
		//
		BeanProperty<BaseInfo, String> leiBie = BeanProperty.create("leiBie");
		jTableBinding.addColumnBinding(leiBie).setColumnName(
				"\u4EBA\u624D\u7C7B\u578B");
		//
		BeanProperty<BaseInfo, String> shiBieHao = BeanProperty.create("shiBieHao");
		jTableBinding.addColumnBinding(shiBieHao).setColumnName(
				"\u4EBA\u624D\u8BC6\u522B\u53F7");
		//
		BeanProperty<BaseInfo, String> xingMing = BeanProperty.create("xingMing");
		jTableBinding.addColumnBinding(xingMing).setColumnName("\u59D3\u540D");
		//
		BeanProperty<BaseInfo, String> xingBie = BeanProperty.create("xingBie");
		jTableBinding.addColumnBinding(xingBie).setColumnName("\u6027\u522B");
		//
		jTableBinding.bind();
		this.getColumnModel().getColumn(0).setMinWidth(120);
		this.getColumnModel().getColumn(1).setMinWidth(280);
		this.getColumnModel().getColumn(2).setMinWidth(140);
	}
}
